package com.iknight.theory.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JeryZym
 */
public class EquipTreeUtils {
    public static synchronized <T extends EquipTreeNode> List<T> getRootNode(List<T> treeNodes) {
        List<T> newTreeNodes = new ArrayList<T>();
        for (T node : treeNodes) {
            if (node.getEquipType().equals("01")) {
                node.setNodes(getChildNodes(node.getId(), treeNodes));
                newTreeNodes.add(node);
            }
        }
        return newTreeNodes;
    }

    public static synchronized <T extends EquipTreeNode> List<T> getFixedRootNode(String equipId, List<T> treeNodes) {
        List<T> newTreeNodes = new ArrayList<T>();
        for (T node : treeNodes) {
            if (node.getEquipId().equals(equipId)) {
                node.setNodes(getChildNodes(node.getId(), treeNodes));
                newTreeNodes.add(node);
            }
        }
        return newTreeNodes;
    }

    public static final <T extends EquipTreeNode> List<T> getChildNodes(String parentId, List<T> treeNodes) {
        List<T> childNodes = new ArrayList<T>();
        for (T node : treeNodes) {
            if (node.getEquipType().equals("03")) {
                if (node.getpId().equals(parentId)) {
                    node.setNodes(getChildNodes(node.getId(), treeNodes));
                    childNodes.add(node);
                }
            }
        }
        return childNodes;
    }
}
